import { BasicColumn, FormSchema } from '@/components/Table';
import { h,ref } from 'vue';
import { Switch } from 'ant-design-vue';
import { enableDisableRole } from '@/api/role/index.ts'
import { usePermission } from '@/hooks/web/usePermission'
import { useMessage } from '@/hooks/web/useMessage';
const { hasBtnPermission } = usePermission();

type CheckedType = boolean | string | number
const pagination = ref({})
export const columns: BasicColumn[] = [
  {
    title: '序号',
    dataIndex: 'index',
    width: 80,
    customRender: ({index}) => {
      const { current = 1, pageSize = 0 } = pagination.value as any
      return ( pageSize * (current - 1) ) + ( index + 1 )
    },
  },
  {
    title: '角色名称',
    dataIndex: 'roleName',
  },
  {
    title: '角色值',
    dataIndex: 'roleCode',
  },
  {
    title: '状态',
    dataIndex: 'status',
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      if (!hasBtnPermission('startOrForbidden')) {
        return ''
      }
      return h(Switch, {
        checked: record.status == 0,
        checkedChildren: '启用',
        unCheckedChildren: '停用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? 0 : 1
          const { createMessage } = useMessage()
          let params = {
            id: record.id,
            status: newStatus
          }
          enableDisableRole(params)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改角色状态`);
            })
            .catch(() => {
              createMessage.error('修改角色状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    customRender: ({ text }) => {
      return text || '--'
    },
  },
  {
    title: '备注',
    dataIndex: 'remark',
    customRender: ({ text }) => {
      return text || '--'
    },
  },
  {
    title: '操作',
    dataIndex: 'actions',
    width: 130,
  }
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'roleName',
    label: '角色名称：',
    labelWidth: 90,
    component: 'Input',
    colProps: { span: 5 },
  },
  {
    field: 'status',
    label: '状态：',
    labelWidth: 90,
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: '0' },
        { label: '停用', value: '1' },
      ],
    },
    colProps: { span: 5 },
  },
];


const alphaNumericRegex = /^[a-zA-Z0-9]*$/;
export const formSchema: FormSchema[] = [
  {
    field: 'roleName',
    label: '角色名称',
    required: true,
    component: 'Input',
  },
  {
    field: 'roleCode',
    label: '角色值',
    required: true,
    rules: [
      {
        trigger: 'blur',
        validator(_, value) {
          if (!alphaNumericRegex.test(value)) {
            return Promise.reject('只能输入字母和数字');
          }
          return Promise.resolve();
        },
      },
    ],
    component: 'Input',
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '启用', value: 0 },
        { label: '停用', value: 1 },
      ],
    },
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
];

export function getPagination(data){
  pagination.value = data
}